package com.beyondsoft.sabg.edu.controller.dataanalysis;

import com.beyondsoft.sabg.edu.model.dto.ChatRequest;
import com.beyondsoft.sabg.edu.service.dataanalysis.AnalysisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.validation.Valid;

/**
 * 对话控制器
 */
@RestController
@RequestMapping("/api/dataanalysis/chat")
@Slf4j
public class ChatController {

    @Autowired
    private AnalysisService analysisService;

    /**
     * 流式对话接口
     */
    @PostMapping("/stream")
    public SseEmitter chatStream(@Valid @RequestBody ChatRequest request) {
        log.info("收到对话请求，查询内容：{}", request.getQuery());
        
        try {
            return analysisService.chatStream(request);
        } catch (Exception e) {
            log.error("对话请求处理失败", e);
            SseEmitter emitter = new SseEmitter();
            try {
                emitter.send(SseEmitter.event()
                    .name("error")
                    .data("对话请求处理失败：" + e.getMessage()));
                emitter.completeWithError(e);
            } catch (Exception ex) {
                log.error("发送错误消息失败", ex);
            }
            return emitter;
        }
    }
} 